package com.controller;

import com.entity.Book;
import com.github.pagehelper.PageInfo;
import com.service.BookService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class BookController {
    @Resource
    private BookService bookService;

    //查询全部
    @RequestMapping("/booklist")
    public String bookList(Model model, String title, String author, @RequestParam(defaultValue = "1") int pageNum){
        PageInfo<Book> pageInfo = bookService.findAllBook(title,author,pageNum);
        if(pageInfo.getList().size()>0){
            //将list存储起来
            model.addAttribute("bookpageInfo",pageInfo);
            model.addAttribute("title",title);
            model.addAttribute("author",author);
            return"booklist";
        }else{
            return"error";
        }
    }
    //查询全部
    @RequestMapping("/managerbooklist")
    public String managerbookList(Model model, String title, String author, @RequestParam(defaultValue = "1") int pageNum){
        PageInfo<Book> pageInfo = bookService.findAllBook(title,author,pageNum);
        if(pageInfo.getList().size()>0){
            //将list存储起来
            model.addAttribute("bookpageInfo",pageInfo);
            model.addAttribute("title",title);
            model.addAttribute("author",author);
            return"managerbooklist";
        }else{
            return"error";
        }
    }
    //查询全部
    @RequestMapping("/readerbooklist")
    public String readerbookList(Model model, String title, String author, @RequestParam(defaultValue = "1") int pageNum){
        PageInfo<Book> pageInfo = bookService.findAllBook(title,author,pageNum);
        if(pageInfo.getList().size()>0){
            //将list存储起来
            model.addAttribute("bookpageInfo",pageInfo);
            model.addAttribute("title",title);
            model.addAttribute("author",author);
            return"readerbooklist";
        }else{
            return"error";
        }
    }

    //新增数据
    @RequestMapping("/add1")
    public String saveBook(Book book){
        bookService.saveBook(book);
        return "redirect:/booklist";
    }

    @RequestMapping("/bookadd")
    public String addBook(){
        return "bookadd";
    }

    //删除数据
    @RequestMapping("/bookdelete/{id}")
    public String deleteBook(@PathVariable Integer id, HttpServletResponse servletResponse) throws IOException {
        int count =bookService.deleteBook(id);
        if(count==1){
            servletResponse.sendRedirect("/booklist");
        }
        return "error";
    }

    //根据id查找
    @GetMapping("/bookupdatePage/{id}")
    public String updatePageBook(Model model,@PathVariable int id){
        Book book = bookService.getBook(id);
        System.out.println("Book object in updatePageBook: " + book);  // 打印 book 对象
        if(book!=null){
            //将list存储起来
            model.addAttribute("book",book);
            return"bookupdate";
        }else{
            return"error";
        }
    }

    //更新数据
    @PostMapping("/bookupdate")
    public String updateBook(Model model,Book book,HttpServletRequest request){
        String id = request.getParameter("id");
        Book bookById = bookService.getBook(Integer.parseInt(id));
        bookService.updateBook(book);
        System.out.println(book);
        return "redirect:/booklist";
    }

    //根据id查找
    @GetMapping("/readercheckbookPage/{id}")
    public String readercheckBook(Model model,@PathVariable int id){
        Book book = bookService.getBook(id);
        System.out.println("Book object in updatePageBook: " + book);  // 打印 book 对象
        if(book!=null){
            //将list存储起来
            model.addAttribute("book",book);
            return"readerbookcheck";
        }else{
            return"error";
        }
    }


    //根据id查找
    @GetMapping("/managerbookupdatePage/{id}")
    public String managerupdatePageBook(Model model,@PathVariable int id){
        Book book = bookService.getBook(id);
        System.out.println("Book object in updatePageBook: " + book);  // 打印 book 对象
        if(book!=null){
            //将list存储起来
            model.addAttribute("book",book);
            return"managerbookupdate";
        }else{
            return"error";
        }
    }
    //更新数据
    @PostMapping("/managerbookupdate")
    public String managerupdateBook(Model model,Book book,HttpServletRequest request){
        String id = request.getParameter("id");
        Book bookById = bookService.getBook(Integer.parseInt(id));
        bookService.updateBook(book);
        System.out.println(book);
        return "redirect:/managerbooklist";
    }

    //删除数据
    @RequestMapping("/managerbookdelete/{id}")
    public String managerdeleteBook(@PathVariable Integer id, HttpServletResponse servletResponse) throws IOException {
        int count =bookService.deleteBook(id);
        if(count==1){
            servletResponse.sendRedirect("/managerbooklist");
        }
        return "error";
    }


}
